﻿using Bangle.BLL;
using Bangle.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Phone_PhoneTypeEdit : BasePage
{
    #region 属性
    /// <summary>
    /// Guid
    /// </summary>
    private string id
    {
        get
        {
            return Request.QueryString["id"] == null ? "" : Request.QueryString["id"].ToString();
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return Request.QueryString["Action"] == null ? "Add" : Request.QueryString["Action"].ToString();
        }
    }
    #endregion

    T_PhoneType model = new T_PhoneType();
    T_PhoneTypeBLL bll = new T_PhoneTypeBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        setUserControlStatus();

        if (!IsPostBack)
        {
            if (Action == "Modify" || Action == "Detail")
            {
                getDate(id);
            }
            if (Action == "Detail")
            {
                this.txt_PhoneTypeName.Enabled = false;
                this.ch_Valid.Enabled = false;

                this.btn_Modify.Visible = true;
                this.btn_Save.Visible = false;
            }
            else if (Action == "Add" || Action == "Modify")
            {
                this.txt_PhoneTypeName.Enabled = true;
                this.ch_Valid.Enabled = true;

                this.btn_Modify.Visible = false;
                this.btn_Save.Visible = true;
            }

        }
    }
    #endregion

    #region 设置用户权限
    private void setUserControlStatus()
    {
        //检验用户权限
        base.CheckUserPermission("002001");
        this.btn_Modify.Enabled = base.CheckUserPermissionOperation("002001", "Edit");
        this.btn_Save.Enabled = base.CheckUserPermissionOperation("002001", "Add") || base.CheckUserPermissionOperation("002001", "Edit"); 
    }
    #endregion

    #region 绑定数据
    /// <summary>
    /// 加载数据
    /// </summary>
    /// <param name="id"></param>
    private void getDate(string id)
    {
        model = bll.GetModel(new Guid(id));
        if (model == null)
        {
            return;
        }
        //this.txt_RoleCode.Text = model.RoleCode;
        this.txt_PhoneTypeName.Text = model.PhoenTypeName;
        this.ch_Valid.Checked = model.Valid == 1;
    }
    #endregion

    #region 提交
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        //检验数据是否重复
        string strCheck = " PhoenTypeName like '" + TextUtility.ReplaceText(this.txt_PhoneTypeName.Text.Trim()) + "'";
        if (id.Length > 0)
        {
            strCheck += " and PhoneTypeId<>'" + id + "'";
        }

        if (bll.GetRecordCount(strCheck) > 0)
        {
            base.Alert("分类名称已存在！");
            return;
        }

        if (Action == "Modify")
        {
            model = bll.GetModel(new Guid(id));
        }
        else if (Action == "Add")
        {
            model = new T_PhoneType();
        }
        else
        {
            base.Alert("异常操作！");
            return;
        }

        model.PhoenTypeName = this.txt_PhoneTypeName.Text.Trim();
        model.ParentId = new Guid();
        model.Valid = this.ch_Valid.Checked ? 1 : 0;


        try
        {
            if (Action == "Modify")
            {
                //修改
                bll.Update(model);
            }
            else if (Action == "Add")
            {
                //保存
                bll.Add(model);
            }

            base.Redirect("操作成功！", "PhoneTypeList.aspx");
        }
        catch (Exception ex)
        {
            base.Alert("操作失败！");
        }
    }
    #endregion

    #region 编辑按钮
    protected void btn_Modify_Click(object sender, EventArgs e)
    {
        Response.Redirect("PhoneTypeList.aspx?Action=Modify&Id=" + id);
    }
    #endregion


}